﻿<%@ Page Title="" Language="C#" MasterPageFile="~/NestedMasterPage.master" %>

<%@ Register TagPrefix="Page" TagName="Nav" Src="~/UserControls/PageControl.ascx" %>
<%@ Register TagPrefix="Page" TagName="SiteMap" Src="~/UserControls/SiteMapControl.ascx" %>

<%@ Import Namespace="System.Web.Script.Serialization" %>

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      //初始化設定
      ControlnInit();
      //取得商品資料
      GenProductList();
    }
  }

  /// <summary>
  /// 初始化設定
  /// <summary>
  private void ControlnInit()
  {
    //初始化每頁顯示商品數為第一選項
    this.lbtnPageSize1.CssClass = "current";
  }

  /// <summary>
  /// 取得商品資料
  /// <summary>
  private void GenProductList()
  {
    string Prod_Cat_ID = Request.QueryString["category"];

    //呈現SiteMap
    SiteMapControl.Prod_Cat_ID = Prod_Cat_ID;

    //每頁顯示商品數
    int pageSize = 15;

    foreach (Control con in this.numsPlaceHolder.Controls)
    {
      if (con is LinkButton && ((LinkButton)con).CssClass == "current")
      {
        pageSize = Convert.ToInt32(((LinkButton)con).Text);
      }
    }
    //計算分頁的筆數區間
    int fromNo = 1 + pageSize * (this.Nav1.currentPageNumber - 1);
    int fromTo = fromNo + pageSize - 1;

    //正反向
    string isASCorDesc = ddlOrderBy.SelectedValue;
    //哩程數區間
    string mileRange = ddlMileRange.SelectedValue;

    B2C_MALL_GetProductList_1 ws = new B2C_MALL_GetProductList_1();
    ws.Url = ConfigurationManager.AppSettings["B2C_MALL_GetProductList_1"].ToString();
    string jsonString = ws.B2C_MALL_GetProductList(System.Configuration.ConfigurationManager.AppSettings["WebDeviceid"].ToString()
      , HttpContext.Current.User.Identity.Name, Prod_Cat_ID
      , fromNo.ToString(), fromTo.ToString(), isASCorDesc
      , mileRange, "1", "999999");
    JObject jObject = JsonConvert.DeserializeObject<JObject>(jsonString);
    if (jObject["Is_Valid"].ToString() == "Y")
    {
      rep.DataSource = jObject["ProductList_Ary"];
      rep.DataBind();
      this.Nav1.TotalCount = this.Nav2.TotalCount = jObject["Total_Count"].ToString();
      this.Nav1.PagesCount = this.Nav2.PagesCount = Math.Ceiling((Convert.ToDecimal(jObject["Total_Count"]) / pageSize)).ToString();
    }
    else
    {
      this.rep.DataSource = null;
      this.rep.DataBind();
      this.Nav1.TotalCount = this.Nav2.TotalCount = "0";
      this.Nav1.PagesCount = this.Nav2.PagesCount = "0";
    }

    //不滿5頁的去除其頁數
    this.Nav1.DisplayPageNumber();
    this.Nav2.DisplayPageNumber();
  }

  /// <summary>
  /// 每頁顯示商品數變更
  /// <summary>
  protected void lbtnPageSize_Click(object sender, EventArgs e)
  {
    //將檢視筆數的顏色先清除
    foreach (Control item in this.numsPlaceHolder.Controls)
    {
      if (item is LinkButton)
      {
        ((LinkButton)item).CssClass = "";
      }
    }
    //從第一頁開始
    this.Nav1.currentPageNumber = 1;
    this.Nav2.currentPageNumber = 1;

    //將postback的檢視筆數設定顏色        
    LinkButton selectButton = ((LinkButton)sender);
    selectButton.CssClass = "current";
    //產生商品清單
    GenProductList();

  }

  /// <summary>
  /// 哩程數區間變更
  /// <summary>
  protected void ddlMileRange_SelectedIndexChanged(object sender, EventArgs e)
  {
    //從第一頁開始
    this.Nav1.currentPageNumber = 1;
    this.Nav2.currentPageNumber = 1;
    GenProductList();
  }

  /// <summary>
  /// 排序方式下拉選單變更
  /// <summary>
  protected void ddlOrderBy_SelectedIndexChanged(object sender, EventArgs e)
  {
    //從第一頁開始
    this.Nav1.currentPageNumber = 1;
    this.Nav2.currentPageNumber = 1;
    GenProductList();
  }

  /// <summary>
  /// 商品列表分頁變更
  /// <summary>
  protected void Nav1_OnPageSelected(object sender)
  {
    if (sender == this.Nav1)
    {
      this.Nav2.currentPageNumber = this.Nav1.currentPageNumber;
    }
    else
    {
      this.Nav1.currentPageNumber = this.Nav2.currentPageNumber;
    }
    //取得欲檢視頁面號碼並產生商品清單 
    GenProductList();
  }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="NestedContent" runat="server">
  <div class="simple-banner">
    <img id="imgBanner" src="~/Images/banners/SimpleBanner/MILEAGES.jpg" alt="哩上加禮館" runat="server" />
  </div>

  <div class="breadcrumbs">
    <Page:SiteMap runat="server" ID="SiteMapControl" />    
  </div>

  <div class="filter-area mileages">
    <div class="filter-left">

      <div class="filter-plane">
        以哩程數顯示商品
        <asp:DropDownList ID="ddlMileRange" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlMileRange_SelectedIndexChanged">
          <asp:ListItem Value="0">不限哩數</asp:ListItem>
          <asp:ListItem Value="1">0-5000哩</asp:ListItem>
          <asp:ListItem Value="2">5001-10000哩</asp:ListItem>
          <asp:ListItem Value="3">10001-15000哩</asp:ListItem>
          <asp:ListItem Value="4">15001-20000哩</asp:ListItem>
          <asp:ListItem Value="5">20001-20000哩以上</asp:ListItem>
        </asp:DropDownList>
      </div>
    </div>
    <div class="filter-mid">
      <div class="drop-filter">
        排序方式 
        <asp:DropDownList ID="ddlOrderBy" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlOrderBy_SelectedIndexChanged">
          <asp:ListItem Value="0">請選擇</asp:ListItem>
          <asp:ListItem Value="1">哩程數由低到高</asp:ListItem>
          <asp:ListItem Value="2">哩程數由高到低</asp:ListItem>
        </asp:DropDownList>
      </div>

    </div>
    <div class="filter-right">

      <div class="num-filter">
        每頁顯示商品數  
        <div class="nums">
          <asp:PlaceHolder ID="numsPlaceHolder" runat="server">            
            <asp:LinkButton ID="lbtnPageSize1" runat="server" OnClick="lbtnPageSize_Click" Text="15"></asp:LinkButton>
            / 
            <asp:LinkButton ID="lbtnPageSize2" runat="server" OnClick="lbtnPageSize_Click">30</asp:LinkButton>
            / 
            <asp:LinkButton ID="lbtnPageSize3d" runat="server" OnClick="lbtnPageSize_Click">60</asp:LinkButton>
          </asp:PlaceHolder>          
        </div>
      </div>
    </div>
  </div>

  <!-- Page Nav User Control -->
  <Page:Nav ID="Nav1" runat="server" OnOnPageSelected="Nav1_OnPageSelected" />

  <div class="product-list">
    <asp:Repeater ID="rep" runat="server">
      <ItemTemplate>
        <div class="product-item">
          <div class="item-image">
            <a id="A1" href='<%# Server.HtmlEncode(Eval("B2C_CODE","~/Products/ProductDetail.aspx?B2C_CODE={0}").ToString()) %>' runat="server">
              <img src='<%# Server.HtmlEncode(Eval("IMAGE_PATH_M").ToString()) %>' alt="#" />
            </a>
          </div>
          <div class="release">
            <%# CommonUtil.GetNEWAndHOT(Server.HtmlEncode(Eval("light_status").ToString())) %>
          </div>
          <div class="description">
            <div class="desc-top">
              <p>特價 $ <b><%# Server.HtmlEncode(Eval("ACT_PRICE","{0:N0}").ToString()) %></b> / 或 </p>
              <p>$ <b><%# Server.HtmlEncode(Eval("CASH").ToString()) %>+<%# Server.HtmlEncode(Eval("MILEAGE").ToString()) %></b>哩</p>
            </div>
            <div class="desc-bottom">
             <%-- <h6>RIMOWA--CLASSIC FLIGHT</h6>--%>
              <p>
                <%# Server.HtmlEncode(Eval("B2C_CODE_NAME").ToString()) %>
              </p>
            </div>
          </div>
        </div>
      </ItemTemplate>
    </asp:Repeater>    
  </div>
  <!-- Page Nav User Control -->
  <Page:Nav ID="Nav2" runat="server" OnOnPageSelected="Nav1_OnPageSelected" />
  <div class="go-to-top">
    <a href="#">回到頂端</a>
  </div>
</asp:Content>
